package com.dzt.androidframework.network.mobapi.cook;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.TitleFrameActivity;
import com.dzt.androidframework.databinding.ActivityCategoryBinding;
import com.dzt.androidframework.network.mobapi.cook.Category.ResultBean.ChildsBeanX;
import com.dzt.androidframework.network.mobapi.cook.Category.ResultBean.ChildsBeanX.ChildsBean;
import com.dzt.androidframework.utils.JLogKit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CategoryActivity extends TitleFrameActivity<ActivityCategoryBinding>
		implements CategoryView {

	private CategoryPresenter presenter;
	private CookExpandableListAdapter selaAdapter;
	private List<Map<String, ChildsBeanX>> categorys = new ArrayList<>();
	private List<List<Map<String, ChildsBean>>> childs = new ArrayList<>();

	@Override
	protected void initData(Bundle savedInstanceState) {
		presenter = new CategoryPresenter();
		presenter.onCreate();
		presenter.attachView(this);
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_category;
	}

	@Override
	protected void initWidgets() {
		setTitle("菜谱大全");
		presenter.queryCategory();
		selaAdapter = new CookExpandableListAdapter(context, R.layout.item_group,
				R.layout.item_child, categorys, childs);
		bindingView.elvCooks.setAdapter(selaAdapter);
		// 设置item点击的监听器
		bindingView.elvCooks.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
										int groupPosition, int childPosition, long id) {

				ChildsBean bean = childs.get(groupPosition).get(childPosition).get("child");
				JLogKit.getInstance().i("name = " + bean.toString());
				Intent intent = new Intent();
				intent.putExtra("cid", bean.getCategoryInfo().getCtgId());
				startActivity(CookMenuActivity.class, intent);
				return false;
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		presenter.onDestroy();
	}

	@Override
	public void onSuccess(Category category) {
		List<ChildsBeanX> childs = category.getResult().getChilds();
		for (ChildsBeanX child : childs) {
			Map<String, ChildsBeanX> group = new HashMap<>();
			group.put("group", child);
			categorys.add(group);

			List<ChildsBean> beans = child.getChilds();
			List<Map<String, ChildsBean>> childes = new ArrayList<>();
			for (ChildsBean bean : beans) {
				Map<String, ChildsBean> ch = new HashMap<>();
				ch.put("child", bean);
				childes.add(ch);
			}
			this.childs.add(childes);
		}
		selaAdapter.notifyDataSetChanged();
	}

	@Override
	public void onError(String result) {

	}
}
